﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OA.UI.MVC.Filters
{
    public class MyActionFilterAttribute:ActionFilterAttribute
    {
        // action执行之前
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            
            //base.OnActionExecuting(filterContext);
            //filterContext.HttpContext    // 通过HttpContext可以获取请求中所有的信息

            var controllerName = filterContext.RouteData.Values["Controller"];
            var actionName = filterContext.RouteData.Values["Action"];

            filterContext.HttpContext.Response.Write($"控制器1：{controllerName},方法1：{actionName}");

            filterContext.Result = new ContentResult() { Content = "到此结束！" };

        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var controllerName =filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            var actionName = filterContext.ActionDescriptor.ActionName;

            filterContext.HttpContext.Response.Write($"控制器2：{controllerName},方法2：{actionName}");
        }

        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            filterContext.HttpContext.Response.Write("结果之后");
        }
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Write("结果之前");
        }
    }
}